package com.yx.frame.builder.xml;

import java.util.ArrayList;
import java.util.List;

public class QueryMethodContentXml extends MethodContentXml {
	List<QuerySql> transaction = new ArrayList<QuerySql>();
	String callback = "";

	public QueryMethodContentXml() {
	}

	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}

	public List<QuerySql> getTransaction() {
		return transaction;
	}
	public List<String> getUserDefineDatasource(){
		List<String> list=new ArrayList<String>();
		for(int i=0;i<transaction.size();i++){
			QuerySql qSql=transaction.get(i);
			if("true".equals(qSql.getUserDefineDatasource())){
				list.add("DatasourceOf"+qSql.getName());
			}
		}
		return list;
	}

	public void addQuerySql(int idx, String name, String sql,
			String unzipcolumn, String datasource, String userDefineDatasource,String sqlColums) {
		QuerySql updateSql = new QuerySql(idx, name, sql, unzipcolumn,
				datasource, userDefineDatasource,sqlColums);
		getTransaction().add(updateSql);
		sort();
	}

	public void sort() {
		QuerySql tmpSql;
		for (int i = 1; i < transaction.size(); i++) {
			for (int j = i; (j > 0)
					&& (transaction.get(j).getIndex() < transaction.get(j - 1)
							.getIndex()); j--) {
				tmpSql = transaction.get(j);
				transaction.set(j, transaction.get(j - 1));
				transaction.set(j - 1, tmpSql);
			}
		}
	}

	public StringBuffer toXml() {
		StringBuffer xml = new StringBuffer();
		xml.append("		<Transaction callback=\"" + getCallback() + "\">\r\n");
		for (int i = 0; i < transaction.size(); i++) {
			QuerySql sql = transaction.get(i);
			xml.append(sql.toXml());
		}
		xml.append("		</Transaction>\r\n");
		return xml;
	}

	public class QuerySql {
		int index = 0;
		String name = "";
		String sql = "";
		String unzipcolumn = "";
		String datasource = "";
		String userDefineDatasource = "false";
		String sqlColums="";

		public String getUserDefineDatasource() {
			return userDefineDatasource;
		}

		public QuerySql(int idx, String name, String sql, String unzipcolumn,
				String datasource, String userDefineDatasource,String sqlCols) {
			sql = sql.replace("\r", "");
			sql = sql.replace("\n", "");
			sql = sql.replace("\t", " ");
			while (sql.startsWith(" ")) {
				sql = sql.substring(1);
			}
			while (sql.endsWith(" ")) {
				sql = sql.substring(0, sql.length() - 1);
			}
			if (null == sqlCols) sqlCols="";
			sqlCols = sqlCols.replace("\r", "");
			sqlCols = sqlCols.replace("\n", "");
			sqlCols = sqlCols.replace("\t", "");
			sqlCols=sqlCols.trim();
			this.index = idx;
			this.name = name;
			this.sql = sql;
			this.unzipcolumn = unzipcolumn;
			if (null != datasource) {
				this.datasource = datasource;
			}
			if (null != userDefineDatasource
					&& "true".equals(userDefineDatasource)) {
				this.userDefineDatasource = "true";
			} else {
				this.userDefineDatasource = "false";
			}
			this.sqlColums=sqlCols;
			
		}

		public int getIndex() {
			return index;
		}

		public String getName() {
			return name;
		}

		public String getSql() {
			return sql;
		}

		public String getUnzipcolumn() {
			return unzipcolumn;
		}

		public int[] getUnzipcolumnNumber() {
			if ("".equals(unzipcolumn))
				return new int[0];
			String[] colArray = getUnzipcolumn().split(",");
			int[] intColArry = new int[colArray.length];
			for (int i = 0; i < colArray.length; i++) {
				intColArry[i] = Integer.parseInt(colArray[i]);
			}
			return intColArry;
		}

		public String getDatasourceStr() {
			return datasource;
		}

		public String[] getDatasource() {
			if ("".equals(datasource)) {
				return new String[0];
			}
			return datasource.split(",");
		}
		public String getSqlColums() {
			return sqlColums;
		}
		public String[] getSqlColumsArray() {
			if("".equals(sqlColums))return new String[0];
			String[] colArray = getSqlColums().split(",");
			for(int i=0;i<colArray.length;i++){
				colArray[i]=colArray[i].trim();
			}
			return colArray;
		}

		public StringBuffer toXml() {
			StringBuffer xml = new StringBuffer();
			xml.append("			<SQL index=\"" + this.getIndex() + "\" name=\""
					+ this.getName() + "\" unzipcolumn=\""
					+ this.getUnzipcolumn() + "\"  datasource=\""
					+ this.getDatasourceStr() + "\"  userDefineDatasource=\""
					+ getUserDefineDatasource() + "\" sqlColums=\""
					+ getSqlColums() + "\">\r\n");
			xml.append("				<![CDATA[" + this.getSql() + "]]>\r\n");
			xml.append("			</SQL>\r\n");
			return xml;
		}

	}

}
